package com.cim.web;

import com.cim.config.GlobalConfig;
import com.cim.pojo.Account;
import com.cim.service.AccountService;
import com.cim.utils.ResponseUtil;
import com.cim.utils.TokenUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Api(tags = "账号相关")
@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    AccountService accountService;

    //登录接口
    @ApiOperation(value="登录接口",notes="登录后获取到的token用于其他接口身份验证")
    @PostMapping("/login")
    public Object login(@RequestParam @ApiParam(value="用户名") String userName,
                        @RequestParam @ApiParam(value="密码") String password) {
        Account account=accountService.getAccountByUserName(userName);
        if(password==null||account==null||!password.equals(account.getPassword())){
            //验证失败
            return ResponseUtil.fail(1,"验证失败");
        }
        String token= TokenUtils.token(userName,password,account.getLecturer());
        Map<Object, Object> result = new LinkedHashMap<>();
        result.put("token",token);
        return ResponseUtil.ok(result);
    }

    //注册接口
    @ApiOperation(value="注册接口")
    @PostMapping("/regist")
    public Object regist(@RequestParam @ApiParam(value="用户名") String userName,
                         @RequestParam @ApiParam(value="密码") String password,
                         @RequestParam(defaultValue="") @ApiParam(value="手机号") String phoneNumber,
                         @RequestParam(defaultValue="") @ApiParam(value="邮箱") String email,
                         @RequestParam @ApiParam(value="是否教师",defaultValue="false") String lecturer){

        Account ac=new Account(null,userName,password,phoneNumber,email,lecturer);
        Account ac2=new Account();
        ac2.setUserName(ac.getUserName());
        List<Account> list=accountService.findAccount(ac2);
        if(list.size()>0){
            return ResponseUtil.fail(1,"用户名已存在");
        }
        String id=accountService.addAccount(ac);
        HashMap<Object,Object> rest=new HashMap<Object,Object>();
        rest.put("userId",id);
        return ResponseUtil.ok(rest);
    }

    @PostMapping("/logout")
    public Object logout() {
        return ResponseUtil.ok();
    }


    //获取账户信息
    @ApiOperation("获取账户信息")
    @PostMapping("/info")
    public Object info(HttpServletRequest request) {
        String username=(String)request.getAttribute(GlobalConfig.TOKEN_USER);
        if(username==null||username.equals("")){
            return ResponseUtil.fail(404,"找不到数据");
        }
        Account ac=new Account();
        ac.setUserName(username);
        List<Account> list=accountService.findAccount(ac);
        if(list==null||list.size()==0){
            return ResponseUtil.fail(404,"找不到数据");
        }
        return ResponseUtil.ok(list.get(0));
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/modifi")
    public Object modifi(@RequestParam @ApiParam("用户Id") String userId,
                         @RequestParam(required=false) @ApiParam("用户名") String userName,
                         @RequestParam(required=false) @ApiParam("密码") String password,
                         @RequestParam(required=false) @ApiParam(value="手机号") String phoneNumber,
                         @RequestParam(required=false) @ApiParam(value="邮箱") String email,
                         @RequestParam(required=false) @ApiParam(value="是否教师",defaultValue="false") String lecturer,
                         HttpServletRequest request){
        String user=(String)request.getAttribute(GlobalConfig.TOKEN_USER);
        Account account=accountService.getAccount(userId);
        if(account==null){
            return ResponseUtil.fail(404,"找不到用户");
        }
        if(!account.getUserName().equals(user)){
            return ResponseUtil.unauthz();
        }
        account.setUserName(userName);
        account.setPassword(password);
        account.setPhoneNumber(phoneNumber);
        account.setEmail(email);
        account.setLecturer(lecturer);
        accountService.updateAccount(account);
        return ResponseUtil.ok(account);
    }

}
